package bb.lanxing.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.NumberPicker;
import androidx.core.content.ContextCompat;
import bb.lanxing.R;
import bb.lanxing.util.DensityUtil;
import java.lang.reflect.Field;

public class SprintDataFieldPicker extends NumberPicker {
    public SprintDataFieldPicker(Context context) {
        super(context);
    }

    public SprintDataFieldPicker(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public SprintDataFieldPicker(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
    }

    public SprintDataFieldPicker(Context context, AttributeSet attributeSet, int i, int i2) {
        super(context, attributeSet, i, i2);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        setNumberPickerDividerColor();
        setNumberPickerDividerHeight();
    }

    @Override
    public void addView(View view, int i, ViewGroup.LayoutParams layoutParams) {
        super.addView(view, i, layoutParams);
        if (view instanceof EditText) {
            ((EditText) view).setTextColor(ContextCompat.getColor(getContext(), R.color.grey_333333));
            view.setEnabled(false);
            view.setFocusable(false);
            view.setFocusableInTouchMode(false);
        }
    }

    public void setNumberPickerDividerHeight() {
//        Field[] declaredFields;
        for (Field field : NumberPicker.class.getDeclaredFields()) {
            if (field.getName().equals("mSelectionDividerHeight")) {
                field.setAccessible(true);
                try {
                    field.set(this, DensityUtil.dp2px(getContext(), 1.0f));
                    return;
                } catch (Resources.NotFoundException | IllegalAccessException |
                         IllegalArgumentException e) {
                    e.printStackTrace();
                    return;
                }
            }
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    public void setNumberPickerDividerColor() {
//        Field[] declaredFields;
        for (Field field : NumberPicker.class.getDeclaredFields()) {
            if (field.getName().equals("mSelectionDivider")) {
                field.setAccessible(true);
                try {
                    field.set(this, new ColorDrawable(ContextCompat.getColor(getContext(), R.color.sprint_watchface_data_field_divider)));
                    return;
                } catch (Resources.NotFoundException | IllegalAccessException |
                         IllegalArgumentException e) {
                    e.printStackTrace();
                    return;
                }
            }
        }
    }
}
